{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Toeplitz Matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: isToeplitzMatrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #托普利茨矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个 <code>m x n</code> 的矩阵 <code>matrix</code> 。如果这个矩阵是托普利茨矩阵，返回 <code>true</code> ；否则，返回<em> </em><code>false</code><em> 。</em></p>\n",
    "\n",
    "<p>如果矩阵上每一条由左上到右下的对角线上的元素都相同，那么这个矩阵是<em> </em><strong>托普利茨矩阵</strong> 。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2020/11/04/ex1.jpg\" style=\"width: 322px; height: 242px;\" />\n",
    "<pre>\n",
    "<strong>输入：</strong>matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]]\n",
    "<strong>输出：</strong>true\n",
    "<strong>解释：</strong>\n",
    "在上述矩阵中, 其对角线为: \n",
    "\"[9]\", \"[5, 5]\", \"[1, 1, 1]\", \"[2, 2, 2]\", \"[3, 3]\", \"[4]\"。 \n",
    "各条对角线上的所有元素均相同, 因此答案是 True 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2020/11/04/ex2.jpg\" style=\"width: 162px; height: 162px;\" />\n",
    "<pre>\n",
    "<strong>输入：</strong>matrix = [[1,2],[2,2]]\n",
    "<strong>输出：</strong>false\n",
    "<strong>解释：</strong>\n",
    "对角线 \"[1, 2]\" 上的元素不同。</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>m == matrix.length</code></li>\n",
    "\t<li><code>n == matrix[i].length</code></li>\n",
    "\t<li><code>1 <= m, n <= 20</code></li>\n",
    "\t<li><code>0 <= matrix[i][j] <= 99</code></li>\n",
    "</ul>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>进阶：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li>如果矩阵存储在磁盘上，并且内存有限，以至于一次最多只能将矩阵的一行加载到内存中，该怎么办？</li>\n",
    "\t<li>如果矩阵太大，以至于一次只能将不完整的一行加载到内存中，该怎么办？</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [toeplitz-matrix](https://leetcode.cn/problems/toeplitz-matrix/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [toeplitz-matrix](https://leetcode.cn/problems/toeplitz-matrix/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[[1,2,3,4],[5,1,2,3],[9,5,1,2]]', '[[1,2],[2,2]]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:\n",
    "        if len(matrix) == 0: return False\n",
    "        if len(matrix) == 1: return True\n",
    "        last_result = self.isToeplitzMatrix(matrix[1:])\n",
    "        return matrix[0][:-1] == matrix[1][1:] and last_result\n",
    "    \n",
    "        # return all(r == 0 or c == 0 or matrix[r-1][c-1] == val\n",
    "        #            for r, row in enumerate(matrix)\n",
    "        #            for c, val in enumerate(row))\n",
    "    \n",
    "        # groups = {}\n",
    "        # for r, row in enumerate(matrix):\n",
    "        #     for c, val in enumerate(row):\n",
    "        #         if r-c not in groups:\n",
    "        #             groups[r-c] = val\n",
    "        #         elif groups[r-c] != val:\n",
    "        #             return False\n",
    "        # return True\n",
    "        # return all(matrix[row+1][1:] == matrix[row][:-1] for row in range(len(matrix)-1))\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isToeplitzMatrix(self, matrix):\n",
    "        \"\"\"\n",
    "        :type matrix: List[List[int]]\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        for i in range(1,len(matrix)):\n",
    "            for j in range(1,len(matrix[i])):\n",
    "                if matrix[i][j]==matrix[i-1][j-1]:\n",
    "                    continue\n",
    "                else:\n",
    "                    return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isToeplitzMatrix(self, matrix):\n",
    "        \"\"\"\n",
    "        :type matrix: List[List[int]]\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        for i in range(len(matrix) - 1):\n",
    "            for j in range(len(matrix[0]) - 1):\n",
    "                if matrix[i][j] != matrix[i+1][j+1]:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isToeplitzMatrix(self, matrix):\n",
    "        \"\"\"\n",
    "        :type matrix: List[List[int]]\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        # rows = len(matrix)\n",
    "        # if rows == 1:\n",
    "        #     return True\n",
    "        # cols = len(matrix[0])\n",
    "        # if cols == 1:\n",
    "        #     return True\n",
    "        # for r in range(1,rows):\n",
    "        #     for c in range(1, cols):\n",
    "        #         cur = matrix[r][c]\n",
    "        #         if (matrix[r-1][c-1] != cur):\n",
    "        #             return False\n",
    "        # return True\n",
    "        \n",
    "        # return all(r == 0 or c == 0 or matrix[r-1][c-1] == val\n",
    "        #            for r, row in enumerate(matrix)\n",
    "        #            for c, val in enumerate(row))\n",
    "        \n",
    "        groups = {}\n",
    "        rows = len(matrix)\n",
    "        cols = len(matrix[0])\n",
    "        for r in range(rows):\n",
    "            for c in range(cols):\n",
    "                val = matrix[r][c]\n",
    "                group = r-c\n",
    "                if group in groups:\n",
    "                    if (val != groups.get(group)):\n",
    "                        return False\n",
    "                else:\n",
    "                    groups[group] = val\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isToeplitzMatrix(self, matrix):\n",
    "        \"\"\"\n",
    "        :type matrix: List[List[int]]\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        # function 88ms\n",
    "        # rows = len(matrix)\n",
    "        # if rows == 1:\n",
    "        #     return True\n",
    "        # cols = len(matrix[0])\n",
    "        # if cols == 1:\n",
    "        #     return True\n",
    "        # for r in range(1,rows):\n",
    "        #     for c in range(1, cols):\n",
    "        #         cur = matrix[r][c]\n",
    "        #         if (matrix[r-1][c-1] != cur):\n",
    "        #             return False\n",
    "        # return True\n",
    "        \n",
    "        # function 84ms\n",
    "        # return all(r == 0 or c == 0 or matrix[r-1][c-1] == val\n",
    "        #            for r, row in enumerate(matrix)\n",
    "        #            for c, val in enumerate(row))\n",
    "        \n",
    "        # function 116ms\n",
    "        # groups = {}\n",
    "        # rows = len(matrix)\n",
    "        # cols = len(matrix[0])\n",
    "        # for r in range(rows):\n",
    "        #     for c in range(cols):\n",
    "        #         val = matrix[r][c]\n",
    "        #         group = r-c\n",
    "        #         if group in groups:\n",
    "        #             if (val != groups.get(group)):\n",
    "        #                 return False\n",
    "        #         else:\n",
    "        #             groups[group] = val\n",
    "        # return True\n",
    "        \n",
    "        m = len(matrix)\n",
    "        n = len(matrix[0])\n",
    "        \n",
    "        for i in range(m-1):\n",
    "            if matrix[i][i:-1]!=matrix[i+1][i+1:]:\n",
    "                return False\n",
    "        for j in range(m-1,1,-1):\n",
    "            if matrix[j][1:]!=matrix[j-1][:-1]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isToeplitzMatrix(self, matrix):\n",
    "        \"\"\"\n",
    "        :type matrix: List[List[int]]\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        if not matrix:\n",
    "            return False\n",
    "        if len(matrix)==1:\n",
    "            return True\n",
    "        if len(matrix[0])==1:\n",
    "            return True\n",
    "\n",
    "        row = len(matrix)\n",
    "        col = len(matrix[0])\n",
    "\n",
    "        if col >= row:\n",
    "            for i in range(col):\n",
    "                test = matrix[0][i]\n",
    "                j = 0\n",
    "                if i < (col - row + 1):\n",
    "                    while(j < row):\n",
    "                        if matrix[j][j+i] != test:\n",
    "                            return False\n",
    "                        j += 1\n",
    "                else:\n",
    "                    while(j < col - i):\n",
    "                        if matrix[j][i+j] != test:\n",
    "                            return False\n",
    "                        j += 1\n",
    "\n",
    "\n",
    "            for i in range(1, row):\n",
    "                test = matrix[i][0]\n",
    "                j = 0\n",
    "                while(j<row-i):\n",
    "                    if matrix[i+j][j] != test:\n",
    "                        return False\n",
    "                    j += 1\n",
    "            return True\n",
    "\n",
    "        else:\n",
    "            for i in range(row):\n",
    "                test = matrix[i][0]\n",
    "                j = 0\n",
    "                if i<(row-col+1):\n",
    "                    print(matrix[i+j][j])\n",
    "                    while(j<col):\n",
    "                        if matrix[i+j][j] != test:\n",
    "                            return False\n",
    "                        j += 1\n",
    "                else:\n",
    "                    while(j < row - i):\n",
    "                        if matrix[i+j][j] != test:\n",
    "                            return False\n",
    "                        j += 1\n",
    "\n",
    "            for i in range(1, col):\n",
    "                test = matrix[0][i]\n",
    "                j = 0\n",
    "                while(j<col-i):\n",
    "                    if matrix[j][i+j] != test:\n",
    "                        return False\n",
    "                    j += 1\n",
    "            return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isToeplitzMatrix(self, matrix):\n",
    "        \"\"\"\n",
    "        :type matrix: List[List[int]]\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        if not matrix:\n",
    "            return False\n",
    "        if len(matrix)==1:\n",
    "            return True\n",
    "        if len(matrix[0])==1:\n",
    "            return True\n",
    "\n",
    "        row = len(matrix)\n",
    "        col = len(matrix[0])\n",
    "\n",
    "        if col >= row:\n",
    "            for i in range(col):\n",
    "                test = matrix[0][i]\n",
    "                j = 0\n",
    "                if i < (col - row + 1):\n",
    "                    while(j < row):\n",
    "                        if matrix[j][j+i] != test:\n",
    "                            return False\n",
    "                        j += 1\n",
    "                else:\n",
    "                    while(j < col - i):\n",
    "                        if matrix[j][i+j] != test:\n",
    "                            return False\n",
    "                        j += 1\n",
    "\n",
    "\n",
    "            for i in range(1, row):\n",
    "                test = matrix[i][0]\n",
    "                j = 0\n",
    "                while(j<row-i):\n",
    "                    if matrix[i+j][j] != test:\n",
    "                        return False\n",
    "                    j += 1\n",
    "            return True\n",
    "\n",
    "        else:\n",
    "            for i in range(row):\n",
    "                print(i)\n",
    "                test = matrix[i][0]\n",
    "                j = 0\n",
    "                if i<(row-col+1):\n",
    "                    print(matrix[i+j][j])\n",
    "                    while(j<col):\n",
    "                        if matrix[i+j][j] != test:\n",
    "                            return False\n",
    "                        j += 1\n",
    "                else:\n",
    "                    while(j < row - i):\n",
    "                        if matrix[i+j][j] != test:\n",
    "                            return False\n",
    "                        j += 1\n",
    "\n",
    "            for i in range(1, col):\n",
    "                print(\"i2\",i)\n",
    "                test = matrix[0][i]\n",
    "                j = 0\n",
    "                while(j<col-i):\n",
    "                    if matrix[j][i+j] != test:\n",
    "                        return False\n",
    "                    j += 1\n",
    "            return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isToeplitzMatrix(self, matrix: 'List[List[int]]') -> 'bool':\n",
    "        \n",
    "        ret = {}\n",
    "        \n",
    "        for r,row in enumerate(matrix):\n",
    "            \n",
    "            for c, val in enumerate(row):\n",
    "                \n",
    "                if r-c not in ret:\n",
    "                    ret[r-c] = val\n",
    "                elif val != ret[r-c]:\n",
    "                    return False\n",
    "                \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:\n",
    "        for i in range(1,len(matrix)):\n",
    "            if matrix[i-1][:-1] != matrix[i][1:]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:\n",
    "        col_len=len(matrix)\n",
    "        row_len=len(matrix[0])\n",
    "        if col_len==1 or row_len==1:\n",
    "            return True\n",
    "        for i in range(len(matrix)-1):\n",
    "            if matrix[i][:-1]!=matrix[i+1][1:]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:\n",
    "        m,n = len(matrix),len(matrix[0])\n",
    "        dic = {}\n",
    "        for r in range(m):\n",
    "            for c in range(n):\n",
    "                if r-c in dic:\n",
    "                    if matrix[r][c] != dic[r-c]:\n",
    "                        return False    \n",
    "                else:\n",
    "                    dic[r-c] = matrix[r][c]\n",
    "        return True\n",
    "\n",
    "                \n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def f(arr):\n",
    "    M = len(arr)\n",
    "    N = len(arr[0])\n",
    "    if M < N:\n",
    "        for i in range(0,N-M+1):\n",
    "            head = arr[0][i]\n",
    "            for x in range(1,M):\n",
    "                if head != arr[x][x+i]:\n",
    "                    return False\n",
    "        for i in range(N-M+1,N):\n",
    "            head = arr[0][i]\n",
    "            for x in range(1,N-i):\n",
    "                if head != arr[x][x+i]:\n",
    "                    return False\n",
    "        for j in range(1,M):\n",
    "            head = arr[j][0]\n",
    "            for y in range(1,M-j):\n",
    "                if head != arr[y+j][y]:\n",
    "                    return False\n",
    "    else:\n",
    "        for j in range(0,M-N+1):\n",
    "            head = arr[j][0]\n",
    "            for y in range(1,N):\n",
    "                if head != arr[y+j][y]:\n",
    "                    return False\n",
    "        for j in range(M-N+1,M):\n",
    "            head = arr[j][0]\n",
    "            for y in range(1,M-j):\n",
    "                if head != arr[y+j][y]:\n",
    "                    return False\n",
    "        for i in range(1,N):\n",
    "            head = arr[0][i]\n",
    "            for x in range(1,N-i):\n",
    "                if head != arr[x][x+i]:\n",
    "                    return False\n",
    "    return True\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:\n",
    "        return f(matrix)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:\n",
    "        row, col = len(matrix), len(matrix[0])\n",
    "        \n",
    "        for i in range(col):\n",
    "            j = 0\n",
    "            cur_num = matrix[j][i]\n",
    "            while j < row and i < col:\n",
    "                if matrix[j][i] != cur_num:\n",
    "                    return False\n",
    "                j, i = j + 1, i + 1\n",
    "        \n",
    "        for i in range(row):\n",
    "            j = 0\n",
    "            cur_num = matrix[i][j]\n",
    "            while i < row and j < col:\n",
    "                if matrix[i][j] != cur_num:\n",
    "                    return False\n",
    "                j, i = j + 1, i + 1\n",
    "        \n",
    "        return True\n",
    "        \n",
    "\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:\n",
    "        m = len(matrix)\n",
    "        n = len(matrix[0])\n",
    "        for i in range(m-1):\n",
    "            for j in range(n-1):\n",
    "                if matrix[i][j] != matrix[i+1][j+1]:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:\n",
    "        for i in range(len(matrix[0])):\n",
    "            if not self.check(matrix, matrix[0][i], 0, i): return False\n",
    "        for i in range(len(matrix)):\n",
    "            if not self.check(matrix, matrix[i][0], i, 0): return False\n",
    "        return True\n",
    "\n",
    "\n",
    "    def check(self,matrix,  target, row, col):\n",
    "        if row >= len(matrix) or col >= len(matrix[0]):\n",
    "            return True\n",
    "        if matrix[row][col] != target:\n",
    "            return False\n",
    "        return self.check(matrix, target, row+1, col+1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:\n",
    "        # 使用字典，元组的位置做键\n",
    "        position_dict = dict()\n",
    "        for row in range(len(matrix)):  # 行\n",
    "            for col in range(len(matrix[0])): # 列\n",
    "                if not row or not col:\n",
    "                    position_dict[(row,col)] = matrix[row][col]\n",
    "                else:\n",
    "                    min_val = min(row,col)\n",
    "                    if matrix[row][col] is not matrix[row-min_val][col-min_val]:\n",
    "                        return False\n",
    "        return True\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:\n",
    "        groups = {}\n",
    "        for r, row in enumerate(matrix):\n",
    "            for c, val in enumerate(row):\n",
    "                print(r)\n",
    "                print(c)\n",
    "                if r-c not in groups:\n",
    "                    groups[r-c] = val\n",
    "                elif groups[r-c] != val:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:\n",
    "        if matrix == []: return False\n",
    "        R = len(matrix)\n",
    "        C = len(matrix[0])\n",
    "        i = R - 1\n",
    "        j = 0\n",
    "        while (i >= 0 and j <= C - 1):\n",
    "            print(i,j)\n",
    "            temp = matrix[i][j]\n",
    "            m, n = i, j\n",
    "            for k in range ((C > R) * (R - i - ((C - j) < R) * (R - C + j)) + (C <= R) * (C - j - ((R - i) < C) * (C - R + i))):\n",
    "                print(matrix[m][n])\n",
    "                if matrix[m][n] != temp:\n",
    "                    return False\n",
    "                m += 1\n",
    "                n += 1\n",
    "            if j == 0 and i > 0:\n",
    "                i -= 1\n",
    "            elif i == 0 and j <= C:\n",
    "                j += 1\n",
    "            else:\n",
    "                break\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:\n",
    "        for i in range(len(matrix)-1):\n",
    "            if matrix[i][:-1] != matrix[i+1][1:]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:\n",
    "        for i in range(len(matrix) - 1):\n",
    "            if matrix[i][:-1] != matrix[i + 1][1:]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:\n",
    "        m,n = len(matrix),len(matrix[0])\n",
    "        for i in range(1,m):\n",
    "            for j in range(1,n):\n",
    "                if matrix[i][j] != matrix[i - 1][j - 1]:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:\n",
    "        for i in range(len(matrix)-1):\n",
    "            for j in range(len(matrix[0])-1):\n",
    "                if matrix[i][j]!=matrix[i+1][j+1]:\n",
    "                    return False\n",
    "                    break\n",
    "                \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:\n",
    "        m,n = len(matrix),len(matrix[0])\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if i+1<m and j+1 < n:\n",
    "                    print(i,j)\n",
    "                    if matrix[i][j] != matrix[i+1][j+1]:\n",
    "                        return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:\n",
    "        leng = len(matrix[0])\n",
    "        broad = len(matrix)\n",
    "        def check(matrix,l,r):\n",
    "            star = matrix[r][l]\n",
    "            while l < leng and r < broad:\n",
    "\n",
    "                if star == matrix[r][l]:\n",
    "                    l += 1\n",
    "                    r += 1\n",
    "                else:\n",
    "\n",
    "                    return False\n",
    "            return True\n",
    "        l,r = 0,0\n",
    "        while l < leng and r < broad:\n",
    "            print(l,r)\n",
    "            if l != leng -1: \n",
    "                if check(matrix,l,r) is False:\n",
    "                    return False\n",
    "                l += 1\n",
    "            elif l == leng -1:\n",
    "                if check(matrix,0,r) is False:\n",
    "                    return False\n",
    "                r += 1\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:\n",
    "        row = len(matrix)\n",
    "        col = len(matrix[0])\n",
    "        for index in range(col):\n",
    "            i = 0\n",
    "            j = index\n",
    "            cur = matrix[i][j]\n",
    "            while i < row and j < col:\n",
    "                if matrix[i][j] != cur:\n",
    "                    return False\n",
    "                    break\n",
    "                else:\n",
    "                    i += 1\n",
    "                    j += 1\n",
    "        if row > 1:\n",
    "\n",
    "            for index in range(1, row):\n",
    "                j = 0\n",
    "                i = index\n",
    "                cur = matrix[i][j]\n",
    "                while i < row and j < col:\n",
    "                    if matrix[i][j] != cur:\n",
    "                        return False\n",
    "                    else:\n",
    "                        i += 1\n",
    "                        j += 1\n",
    "        \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:\n",
    "        # Check each element against its top-left neighbor\n",
    "        for i in range(1, len(matrix)):\n",
    "            for j in range(1, len(matrix[0])):\n",
    "                if matrix[i][j] != matrix[i-1][j-1]:\n",
    "                    return False\n",
    "        return True\n",
    "\n",
    "# Test Case 1\n",
    "matrix1 = [[1,2,3,4],[5,1,2,3],[9,5,1,2]]\n",
    "sol = Solution()\n",
    "result1 = sol.isToeplitzMatrix(matrix1)\n",
    "print(result1)  # Expected Output: True\n",
    "\n",
    "# Test Case 2\n",
    "matrix2 = [[1,2],[2,2]]\n",
    "result2 = sol.isToeplitzMatrix(matrix2)\n",
    "print(result2)  # Expected Output: False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:\n",
    "        m=len(matrix)\n",
    "        n=len(matrix[0])\n",
    "        minus=True\n",
    "        for i in range(m-1):\n",
    "            for j in range(n-1):\n",
    "                if matrix[i][j]!=matrix[i+1][j+1]:\n",
    "                    minus=False\n",
    "                    break\n",
    "        return minus\n",
    "\n",
    "            \n",
    "\n",
    "\n",
    "                    \n",
    "\n",
    "            "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
